#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

MQ::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment
{
public:
    void SetUp() override
    {
        LOG(DEBUG, "测试环境初始化ing...\n");
        emp = std::make_shared<MQ::ExchangeManager>("./data/meta.db");
    }

    void TearDown() override
    {
        LOG(DEBUG, "测试环境清理ing...\n");
        emp->clear();
    }

};

TEST(exchangetest, insert_test)
{
    std::unordered_map<std::string, std::string> emap;
    emp->declareExchange("exchange1", MQ::ExchangeType::DIRECT, true, false, emap);
    emp->declareExchange("exchange2", MQ::ExchangeType::TOPIC, true, false, emap);
    emp->declareExchange("exchange3", MQ::ExchangeType::BROADCAST, true, false, emap);
    emp->declareExchange("exchange4", MQ::ExchangeType::DIRECT, true, false, emap);

    ASSERT_EQ(emp->size(), 4);
}

TEST(exchangetest, select_test)
{
    ASSERT_EQ(emp->exists("exchange1"), true);
    ASSERT_EQ(emp->exists("exchange2"), true);
    ASSERT_EQ(emp->exists("exchange3"), true);
    ASSERT_EQ(emp->exists("exchange4"), true);
    MQ::Exchange::ptr exp = emp->selectExchange("exchange2");
    ASSERT_NE(exp.get(), nullptr);
    ASSERT_EQ(exp->name, "exchange2");
    ASSERT_EQ(exp->durable, true);
    ASSERT_EQ(exp->auto_delete, false);
    ASSERT_EQ(exp->type,MQ::ExchangeType::TOPIC);
    ASSERT_EQ(exp->getArgs(), std::string());
}

TEST(exchangetest, delete_test)
{
    emp->deleteExchange("exchange2");
    MQ::Exchange::ptr exp = emp->selectExchange("exchange2");
    ASSERT_EQ(exp.get(), nullptr);
    ASSERT_EQ(emp->exists("exchange2"), false);
}


int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();

    return 0;   
}